package com.zhiyou100.service;

import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhiyou100.dao.ProjectDao;
import com.zhiyou100.model.Project;
import com.zhiyou100.vo.PageVo;

@Service
public class ProjectServiceImpl implements ProjectService{

	@Autowired
	private ProjectDao projectDao;

	@Override
	public PageVo<Project> list(Project project) {
		int page = project.getPage();

		int size = project.getSize();

		// 获取总条数
		int count = projectDao.count();

		if (count == 0) {

			// 没有数据
			return new PageVo<>(1, size, 0, 0, null, 0, 0, 0);
		} else {

			// 计算总页数
			int pageCount = (count + size - 1) / size;

			if (page < 1) {

				page = 1;
			}
			if (page > pageCount) {

				page = pageCount;
			}

			project.setPage((page - 1) * size);
			project.setSize(size);
			int categoryId = project.getCategoryId();
			int status = project.getStatus();
			int sort = project.getSort();

			ArrayList<Project> list = projectDao.find(project);

			return new PageVo<>(page, size, pageCount, count, list, categoryId, status, sort);
		}}

	@Override
	public Project find(int id) {
		
		return projectDao.findAll(id);
	}

	@Override
	public void add(Project project) {

		
		projectDao.add(project);
	}

	@Override
	public void update(Project project) {

		projectDao.update(project);
		
	}

	@Override
	public void updateStatus(Project project) {

		projectDao.updateStatus(project);
		
	}

	@Override
	public void delete(int id) {

		projectDao.delete(id);
		
	}
	
	


}
